package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.RedisConst;
import com.atguigu.gmall.model.product.*;

import com.atguigu.gmall.model.to.CategoryAndChildTo;
import com.atguigu.gmall.product.mapper.BaseCategory1Mapper;
import com.atguigu.gmall.product.mapper.BaseCategory2Mapper;
import com.atguigu.gmall.product.mapper.BaseCategory3Mapper;
import com.atguigu.gmall.product.service.BaseCategoryService;
import com.atguigu.gmall.starter.cache.CacheService;
import com.atguigu.gmall.starter.cache.aop.annotation.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseCategoryImpl implements BaseCategoryService {
    @Autowired
    BaseCategory1Mapper baseCategory1Mapper;
    @Autowired
    BaseCategory2Mapper baseCategory2Mapper;
    @Autowired
    BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    CacheService cacheService;


    /**
     * 获取一级列表
     * @return
     */
    @Cache(cacheKey = "category:level:1")
    @Override
    public List<BaseCategory1> findbaseCategory1() {


        List<BaseCategory1> category1List = baseCategory1Mapper.selectList(null);
        return category1List;
    }

    /**
     * 获取二级列表
     * @param id
     * @return
     */
    @Cache(cacheKey = "category:level:2:#{#args[0]}")
    @Override
    public List<BaseCategory2> findbaseCategory2ByC1id(Long id) {
        QueryWrapper<BaseCategory2> baseCategory2QueryWrapper = new QueryWrapper<>();
        baseCategory2QueryWrapper.eq("category1_id",id);
        List<BaseCategory2> category2List = baseCategory2Mapper.selectList(baseCategory2QueryWrapper);
        return category2List;
    }

    /**
     * 获取三级列表
     * @param category2Id
     * @return
     */
    @Cache(cacheKey = "category:level:3:#{#args[0]}")
    @Override
    public List<BaseCategory3> findbaseCategory3ByC2id(Long category2Id) {
        QueryWrapper<BaseCategory3> baseCategory3QueryWrapper = new QueryWrapper<>();
        baseCategory3QueryWrapper.eq("category2_id",category2Id);
        List<BaseCategory3> category3List = baseCategory3Mapper.selectList(baseCategory3QueryWrapper);
        return category3List;
    }

    /**
     * 获取所有分类及子分类数据
     * @return
     */
    @Cache(cacheKey = RedisConst.CATEGORY_CACHE_KEY)
    @Override
    public List<CategoryAndChildTo> getAllCategoryWithChilds() {
        //1.尝试查询缓存
        List<CategoryAndChildTo> cacheData = cacheService.getCacheData(RedisConst.CATEGORY_CACHE_KEY, new TypeReference<List<CategoryAndChildTo>>() {
        });


       if(cacheData == null){
           //2.查询数据库
           List<CategoryAndChildTo> categoryWithChilds = baseCategory1Mapper.getAllCategoryWithChilds();
//            //把查询出的放入缓存
//            cacheService.saveCategory(RedisConst.CATEGORY_CACHE_KEY,categoryWithChilds);
           return categoryWithChilds;
//
        }
        //返回缓存数据
       return   cacheData;

    }

    /**
     * 获取三级列表层级
     * @param skuId
     * @return
     */
    @Override
    public BaseCategoryView getSkuCategoryView(Long skuId) {

        BaseCategoryView view=baseCategory1Mapper.getSkuCategoryView(skuId);
        return view;
    }


}
